const app = getApp()

Page({
    data: {
        users: !0,
        score: 0,
        balance: 0,
        coupon_length: 0,
        resdata: {}
    },
    onLoad: function (e) {
        app.getUrl(this), app.getSystem(this)
        this.init()
    },
    init() {
        wx.request({
            url: app.siteInfo.siteroot_web,
            cachetime: '0',
            method: 'GET',
            data: {
                c: 'my', // 路径参数，去掉do
                a: 'information',
                user_id: app.globalData.userId
            },
            success: origin => {
                const res = origin.data
                console.log('res--', res)
                if (res.code == 0) {
                    // 请求成功
                    this.setData({
                        resdata: res.data
                    })
                } else {
                    // 请求失败
                    wx.showToast({
                        icon: 'none',
                        title: res.msg
                    })
                }
            }
        })
    },
    refresh: function (e) {
        var t = this,
            n = app.globalData.userId
        app.util.request({
                url: 'entry/wxapp/MyCoupons',
                cachetime: '0',
                data: {
                    user_id: n
                },
                success: function (e) {
                    t.setData({
                        coupon_length: e.data.length
                    })
                }
            }),
            app.util.request({
                url: 'entry/wxapp/CheckRz',
                data: {
                    user_id: n
                },
                success: function (e) {
                    t.setData({
                        state: e.data
                    })
                }
            }),
            app.util.request({
                url: 'entry/wxapp/GetFxSet',
                success: function (e) {
                    t.setData({
                        GetFxSet: e.data
                    })
                }
            })
    },
    mine_order: function (e) {
        if (1 == this.data.users) {
            var t = e.currentTarget.dataset.index,
                n = t
            wx.switchTab({
                url: '../order/order?activeIndex=' +
                    n +
                    '&index=' +
                    t +
                    '&status=' +
                    e.currentTarget.dataset.status
            })
        } else this.bind_user_info()
    },
    home: function (e) {
        wx.reLaunch({
            url: '../index/index'
        })
    },
    seller_manage: function (e) {
        if (1 == this.data.users) {
            wx.getStorage({
                key: 'sign',
                success(res) {
                    console.log(res, '看看res是什么')
                    wx.navigateTo({
                        url: '../logs/Workbench'
                    })
                    //   wx.redirectTo({
                    //     url: "../logs/Workbench"
                    // })
                },
                fail(err) {
                    console.log(err, 'err是什么')
                    wx.navigateTo({
                        url: '../login_entry/login_entry'
                    })
                }
            })
        } else {
            this.bind_user_info()
        }
    },
    xykh: function (e) {
        1 == this.data.users ?
            wx.navigateTo({
                url: '../xykh/xykh'
            }) :
            this.bind_user_info()
    },
    opensetting: function (e) {
        wx.openSetting({})
    },
    integral: function (e) {
        1 == this.data.users ?
            wx.switchTab({
                url: '../jifen/jifen'
            }) :
            this.bind_user_info()
    },
    settled_store: function (e) {
        1 == this.data.users ?
            wx.navigateTo({
                url: '../settled_store/settled_store'
            }) :
            this.bind_user_info()
    },
    service: function (e) {
        1 == this.data.users ?
            wx.navigateTo({
                url: '../service/service'
            }) :
            this.bind_user_info()
    },
    coupon: function (e) {
        1 == this.data.users ?
            wx.navigateTo({
                url: '../logs/hbmx'
            }) :
            this.bind_user_info()
    },
    receive_coupon: function (e) {
        1 == this.data.users ?
            wx.navigateTo({
                url: '../coupon/receive_coupon'
            }) :
            this.bind_user_info()
    },
    getUserProfile(e) {
        // 推荐使用wx.getUserProfile获取用户信息，开发者每次通过该接口获取用户个人信息均需用户确认
        // 开发者妥善保管用户快速填写的头像昵称，避免重复弹窗
        let u = wx.getStorageSync('userInfo')
        wx.getUserProfile({
            desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: res => {
                u.name = res.userInfo.nickName
                u.img = res.userInfo.avatarUrl
                this.setData({
                    userInfo: u
                    // hasUserInfo: true
                })
                wx.setStorageSync('userInfo', u)
                app.util.request({
                    url: 'entry/wxapp/UpdateUserInfo',
                    cachetime: '0',
                    data: {
                        user_id: u.id,
                        name: u.name,
                        img: u.img
                    },
                    success: function (t) {
                        // wx.showToast({
                        //   title: '获取成功1',
                        // })
                    }
                })
                // wx.getStorageInfoSync('userInfo',u)
            }
        })
    },
    user_zhuce(e) {
        if (!app.globalData.hasRegister) {
            // 判断是不是会员，
            wx.navigateTo({
                url: '../register/register'
            })
            return
        }

        var n = wx.getStorageSync('userInfo')
        var t = this.data,
            a = t.platform.open_member
        //未授权登录
        if (n == null || n.name == '微信用户') {
            this.getUserProfile()
            return
        }

        1 == a && null != a ?
            !app.globalData.hasRegister ?
            wx.navigateTo({
                url: '../register/register'
            }) :
            wx.navigateTo({
                // url: "member"
            }) :
            wx.showModal({
                title: '温馨提示',
                content: '平台未开放注册会员,请联系平台管理员'
            })
    },
    user_zhuce1: function (e) {
        var n = wx.getStorageSync('userInfo')
        var t = this.data,
            a = t.platform.open_member
        //未授权登录

        1 == a && null != a ?
            !app.globalData.hasRegister ?
            wx.navigateTo({
                url: '../register/register'
            }) :
            wx.navigateTo({
                url: 'member'
            }) :
            wx.showModal({
                title: '温馨提示',
                content: '平台未开放注册会员,请联系平台管理员'
            })
    },
    cancel_storange: function (e) {
        wx.clearStorage(),
            wx.showToast({
                title: '清除成功'
            })
    },
    copyright: function (e) {
        var t = wx.getStorageSync('platform').tz_appid
        wx.navigateToMiniProgram({
            appId: t,
            success: function (e) {}
        })
    },
    recharge: function (e) {
        1 == this.data.users ?
            wx.navigateTo({
                url: '../recharge/index'
            }) :
            this.bind_user_info()
    },
    mine_mall: function (e) {
        // 1 == this.data.users ? wx.navigateTo({
        //     url: "../jifen/jifen"
        // }) : this.bind_user_info();

        1 == this.data.users ?
            wx.switchTab({
                url: '../jifen/jifen'
            }) :
            this.bind_user_info()
    },
    score_detail: function (e) {
        1 == this.data.users ?
            wx.navigateTo({
                url: '../jifen/scoredetails'
            }) :
            this.bind_user_info()
    },
    coupons_detail() {
        wx.navigateTo({
            url: '/zh_jdgjb/pages/coupons/index'
        })
    },
    zhibo: function (e) {
        1 == this.data.users ?
            wx.navigateTo({
                url: '../liveplay/index'
            }) :
            this.bind_user_info()
    },
    yhxy: function (e) {
        1 == this.data.users ?
            wx.navigateTo({
                url: 'yhxy'
            }) :
            this.bind_user_info()
    },

    // 用户类型 2推销员 3业务员
    distribution: function (e) {
        app.globalData.userType = 2

        wx.setScreenBrightness({
            // 设置屏幕亮度
            value: 0.5
        })

        if (!app.globalData.hasRegister) {
            // 判断是不是会员，
            wx.showModal({
                content: '您需要注册会员',
                success: function (e) {
                    e.confirm &&
                        wx.navigateTo({
                            url: '../register/register'
                        })
                }
            })
            return
        }
        // 0.未申请;1.审核中;2.通过;3.拒绝;
        if (this.data.resdata.distribution_status == 0) {
            // 未申请
            wx.navigateTo({
                url: '/zh_jdgjb/pages/becomesale/becomesale'
            })
        } else if (this.data.resdata.distribution_status == 1) {
            wx.showModal({
                title: '温馨提示',
                content: '提交成功，系统正在审核中，请耐心等待',
                showCancel: false,
                confirmText: '我知道了',
                confirmColor: '#FF9900'
            })
        } else if (this.data.resdata.distribution_status == 2) {
            wx.navigateTo({
                url: '/zh_jdgjb/pages/saleindex/saleindex'
            })
        } else {
            wx.showModal({
                title: '温馨提示',
                content: '您的申请已被拒绝',
                showCancel: false,
                confirmText: '我知道了',
                confirmColor: '#FF9900'
            })
        }
    },
    onShow: function () {
        wx.setScreenBrightness({
            // 设置屏幕亮度
            value: 0.5
        })
        this.init()
        var t = this
        app.getUserInfo(function (e) {
            t.setData({
                    userInfo: e,
                    balance: e.balance,
                    score: Number(e.score)
                }),
                t.refresh()
        })
        this.setTabBarSelected()
    },
    /**
     * @function 设置自定义tabbar选中状态
     */
    setTabBarSelected() {
        if (typeof this.getTabBar === 'function') {
            this.getTabBar(tabBar => {
                tabBar.setData({
                    selected: 3
                })
            })
        }
    },
    // 用户类型 2推销员 3业务员
    busy_manage() {
        // 跳转到业务员页面
        app.globalData.userType = 3
        wx.navigateTo({
            url: '/zh_jdgjb/pages/saleindexbusy/saleindexbusy'
        })
    },
    test() {
        wx.navigateTo({
            url: '/zh_jdgjb/pages/pointsMall/productDetails/index'
        })
    },
    test1() {
        wx.navigateTo({
            url: '/zh_jdgjb/pages/pointsMall/shopCart/index'
        })
    },
    toAgent() {
        const information = this.data.resdata
        wx.navigateTo({
            url: `/zh_jdgjb/pages/agents/index?information=${JSON.stringify(information)}`
        })
    },
    toPartnerships() {
        const information = this.data.resdata
        wx.navigateTo({
            url: `/zh_jdgjb/pages/partnerships/index?information=${JSON.stringify(information)}`
        })
    }
})